package mobilesmil.xml.generating;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import mobilesmil.data.SmilDataSet;
import mobilesmil.data.SmilRegion;
import mobilesmil.data.SmilResource;
import android.os.Environment;

/**
 * Utilities for generating a SMIL XML document from a {@link SmilDataSet} and saving the XML document to the 
 * device's SD card directory.
 * @author Ryan Ware
 *
 */
public class SmilXmlGenerator {

	public static void saveAsXML(SmilDataSet smilDataSet) throws Exception {
		saveAsXML(smilDataSet, smilDataSet.getFileName());
	}
	
	/**
	 * Generates a SMIL XML document from a {@link SmilDataSet} and saves it to the device's SD card directory.
	 * @param smilDataSet {@link SmilDataSet} to be saved as XML
	 * @param fileName {@link String} file name of the XML file to be saved
	 * @throws Exception if file cannot be written to the SD card directory
	 */
	public static void saveAsXML(SmilDataSet smilDataSet, String fileName) throws Exception
	{
		String SDCardDir = Environment.getExternalStorageDirectory() + "/";
		if(fileName.startsWith(SDCardDir))
			fileName = fileName.replaceFirst(SDCardDir, "");
		if(!fileName.endsWith(".smil"))
			fileName += ".smil";
		
		File root = Environment.getExternalStorageDirectory();
		File xmlFile = new File(root, fileName);
		xmlFile.createNewFile();
		
		FileWriter fileWriter = new FileWriter(xmlFile);
        BufferedWriter out = new BufferedWriter(fileWriter);
        
        StringBuilder regionsXmlBuilder = new StringBuilder();
        StringBuilder parXmlBuilder = new StringBuilder();
        
        ArrayList<SmilResource> resources = new ArrayList<SmilResource>();
        resources.addAll(smilDataSet.getResourcesByBeginTime());
        
        // this loop builds a par tag and all resource tags within it
        // and all of the region tags
        for(int i = 0; i < resources.size(); i++)
        {
        	if(i == 0) // first resource
        	{
        		parXmlBuilder.append("<par>\n");
        	}
        	
        	regionsXmlBuilder.append( parseRegionXml(resources.get(i).getSmilRegion()) );
        	parXmlBuilder.append( parseResourceXml(resources.get(i)) );
        	
        	if(i == resources.size() - 1) // last resource
        	{
        		parXmlBuilder.append("</par>\n");
        	}
        }
        // put pieces together
        StringBuilder overallXmlBuilder = new StringBuilder();
        overallXmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE smil SYSTEM \"smil_dtd_subset.dtd\">\n");
        overallXmlBuilder.append("<smil>\n<head>\n<layout>\n");
        overallXmlBuilder.append( parseRootLayoutXml(smilDataSet) );
        overallXmlBuilder.append( regionsXmlBuilder.toString() );
        overallXmlBuilder.append("</layout>\n</head>\n<body>\n");
        overallXmlBuilder.append( parXmlBuilder.toString() );
        overallXmlBuilder.append("</body>\n</smil>");
        
        out.write( overallXmlBuilder.toString() );
        out.close();

	}
	
	/**
	 * Pull {@link SmilResource} parameters and place in XML.
	 * @param resource {@link SmilResource} resource
	 * @return {@link String} XML representing the resource
	 */
	private static String parseResourceXml(SmilResource resource)
	{
		String xml = "<" + resource.getType() 
			+ " src=\"" + resource.getSource()
			+ "\" region=\"" + resource.getSmilRegion().getId()
			+ "\" begin=\"" + resource.getBeginTime()
			+ "\" end=\"" + resource.getEndTime()
			+ "\" />\n"
		;
		return xml;
	}
	
	/**
	 * Pull {@link SmilRegion} parameters and place in XML.
	 * @param region {@link SmilRegion} region
	 * @return {@link String} XML representing the region
	 */
	private static String parseRegionXml(SmilRegion region)
	{
		String xml = "<region id=\"" + region.getId()
			+ "\" top=\"" + region.getRect().top
			+ "\" left=\"" + region.getRect().left
			+ "\" width=\"" + region.getRect().width()
			+ "\" height=\"" + region.getRect().height()
			+ "\" />\n"
		;
		return xml;
	}
	
	/**
	 * Pull {@link SmilDataSet} parameters and place in XML.
	 * @param dataSet {@link SmilDataSet} data set
	 * @return {@link String} XML representing the data set
	 */
	private static String parseRootLayoutXml(SmilDataSet dataSet)
	{
		String xml = "<root-layout height=\"" + dataSet.getCanvasHeight() 
			+ "\" width=\"" + dataSet.getCanvasWidth()
			+ "\" background-color=\"" + dataSet.getBackgroundColorHexString() 
			+ "\" />\n"
		;
		return xml;
	}
}
	
